import { queryPage, queryInfoById, create, lock, update, queryList, queryFloorById, queryLocation, remove } from './service';
import { queryDictList } from '@/services/dict';
import { routerRedux } from 'dva/router';
import { DICT, LOCATION } from './enum';

function setList(list, index) {
  list[index].status = list[index].status === 'lock' ? 'normal' : 'lock';
  return list;
}

export default {
  namespace: 'position_xl',

  state: {
    list: { data: [] },
    details: [],
    cond: {},
    dict: {},
    location: []
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.position_xl.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      yield put({ type: 'queryDictList' });
      yield put({ type: 'setPage', payload: { list, total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryDictList({ payload }, { call, put }) {  // 下拉列表查
      const dictList = yield call(queryList, payload);
      yield put({ type: 'setDictList', payload: dictList })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items = {};
      payload.keys.forEach((item: string) => {
        if (item === 'location') {
          items['tLocation'] = response[item];
        } else {
          items[item] = response[item]
        }
      });

      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      yield put(routerRedux.push('/data/position'));
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/position'));
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload);
      yield put({ type: 'queryPage' });
    },
    *lock({ payload }, { all, call, put }) {
      yield all([
        call(lock, payload.id),
        put({ type: 'setOneLock', payload: payload.ind })
      ]);
    },
    *queryLocation({ payload }, { call, put, select }) {
      const { option = [], target } = payload || {};
      const len = option.length;
      const response = yield call(queryLocation, {
        tail: LOCATION[len].tail,
        ...(len > 0 ? { [`${LOCATION[len - 1].type}Id`]: target.id } : {})
      });

      const loc = yield select(state => state.position_xl.location);
      const ans = Array.isArray(response) ? response.map((v, ind) => ({
        label: v[LOCATION[len].name],
        value: `${v.id}%%${ind}`,
        isLeaf: !(len < 2),
      })) : [];

      let it = loc
      option.forEach(v => {
        it = it[v].children ? it[v].children : it[v]
      })

      it.children = ans;
      it.loading = false;
      yield put({ type: 'setLocation', payload: loc.length === 0 ? ans : loc })
    },
    *init({ payload }, { put }) {
      yield put({ type: 'upload/setImgValue', payload: { url: payload.url } });
      yield put({ type: 'queryLocation' })
    },
    *queryFloorImg({ payload }, { call, put }) {
      const response = yield call(queryFloorById, payload);
      yield put({ type: 'setFloorImg', payload: response.floorPlan })
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setDictList(state, action) {
      return {
        ...state,
        dictList: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setOneLock(state, action) {
      return {
        ...state,
        list: {
          ...state.list,
          data: setList(state.list.data, action.payload)
        }
      }
    },
    setLocation(state, action) {
      return {
        ...state,
        location: action.payload
      }
    },
    setFloorImg(state, action) {
      return {
        ...state,
        image: action.payload
      }
    }
  }
}
